package com.zcx.qshop.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.zcx.helper.bound.BoundView;
import com.zcx.helper.bound.BoundViewHelper;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilToast;
import com.zcx.qshop.QSApplication;
import com.zcx.qshop.R;
import com.zcx.qshop.adapter.ConfirmOrderAdapter;
import com.zcx.qshop.conn.JsonAddressAsyGet;
import com.zcx.qshop.conn.JsonCouponAsyGet;
import com.zcx.qshop.conn.JsonOrderresultAsyGet;
import com.zcx.qshop.conn.JsonOrdersubmitAsyGet;
import com.zcx.qshop.dialog.WhetherDialog;
import com.zcx.qshop.pay.PayAction;
import com.zcx.qshop.view.CouponView;
import com.zcx.qshop.view.TitleView;

import java.text.DecimalFormat;

/**
 * Created by Administrator on 2015/8/23 0023.
 */
public class ConfirmOrderActivity extends QSActivity implements View.OnClickListener{

    public static OnOrderChangeListener onOrderChangeListener;

    @BoundView(R.id.title_view) private TitleView titleView;

    @BoundView(R.id.confirm_order_layout_one) private View layout_one;

    @BoundView(R.id.confirm_order_add_address) private View add_address;

    @BoundView(R.id.confirm_order_edit_address) private View edit_address;

    @BoundView(R.id.confirm_order_username) private TextView username;

    @BoundView(R.id.confirm_order_mobile) private TextView mobile;

    @BoundView(R.id.confirm_order_location) private TextView location;

    @BoundView(R.id.confirm_order_list_view) private ListView listView;

    @BoundView(R.id.confirm_order_coupon_view) private CouponView couponView;

    @BoundView(R.id.confirm_order_order_buyremark) private EditText buyremark;

    @BoundView(R.id.confirm_order_order_price) private TextView orderPrice;

    @BoundView(R.id.confirm_order_coupon_price) private TextView couponPrice;

    @BoundView(R.id.confirm_order_yunfei_price) private TextView yunfeiPrice;

    @BoundView(R.id.confirm_order_count) private TextView count;

    @BoundView(R.id.confirm_order_totle) private TextView totle;

    @BoundView(R.id.confirm_order_coupon) private View coupons;

    @BoundView(R.id.confirm_order_confirm) private View confirm;

    private DecimalFormat decimalFormat = new DecimalFormat("0.00");

    private JsonOrdersubmitAsyGet.Info info;

    private JsonCouponAsyGet.Info.Coupon coupon = new JsonCouponAsyGet.Info.Coupon();

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_confirm_order);

        info = (JsonOrdersubmitAsyGet.Info) getIntent().getSerializableExtra("Info");

        titleView.showStyle(TitleView.Style.FULL);

        titleView.showType(TitleView.Type.BACK_TWO, TitleView.Type.NAME_ONE);

        titleView.setTitleName("确认订单");

        titleView.setOnTitleItemClickListener(new TitleView.OnTitleItemClickListener() {

            @Override
            public void onBackClick() {

                finish();

            }

        });

        listView.addHeaderView(BoundViewHelper.boundView(this, QSApplication.ScaleScreenHelper.loadView((ViewGroup) getLayoutInflater().inflate(R.layout.header_confirm_order, null))));

        listView.addFooterView(BoundViewHelper.boundView(this, QSApplication.ScaleScreenHelper.loadView((ViewGroup) getLayoutInflater().inflate(R.layout.footer_confirm_order, null))));

        listView.setAdapter(new ConfirmOrderAdapter(this, info.goods));

        onOrderChangeListener = new OnOrderChangeListener() {

            @Override
            public void onCouponChange(JsonCouponAsyGet.Info.Coupon coupon) {

                ConfirmOrderActivity.this.coupon = coupon;

                couponView.setVisibility(View.VISIBLE);

                couponView.setLoad(coupon.price, coupon.fullprice, coupon.endtime ,coupon.statue.equals("-1"));

                if(Double.valueOf(info.totalprice) > Double.valueOf(coupon.fullprice)){

                    couponPrice.setText(coupon.price + "元");

                    totle.setText(decimalFormat.format(Double.valueOf(info.totalprice) - Double.valueOf(coupon.price)));

                }else{

                    couponPrice.setText("0元");

                }

            }

            @Override
            public void onAddressChange(JsonAddressAsyGet.Info.Address address) {

                layout_one.setVisibility(View.GONE);

                edit_address.setVisibility(View.VISIBLE);

                username.setText(address.name);

                mobile.setText(address.mobile);

                location.setText(address.city + " " + address.address);

            }

        };

        layout_one.setVisibility(info.address.equals("") ? View.VISIBLE : View.GONE);

        edit_address.setVisibility(info.address.equals("") ? View.GONE : View.VISIBLE);

        edit_address.setOnClickListener(this);

        add_address.setOnClickListener(this);

        couponView.setOnClickListener(this);

        confirm.setOnClickListener(this);

        coupons.setOnClickListener(this);

        username.setText(info.name);

        mobile.setText(info.mobile);

        location.setText(info.address);

        orderPrice.setText(info.totalprice + "元");

        yunfeiPrice.setText("满" + info.yunfei + "元起送");

        count.setText("共" + info.count + "件商品");

        totle.setText(info.totalprice);

    }

    @Override
    public void onClick(final View v) {

        switch (v.getId()){

            case R.id.confirm_order_coupon:

                startVerifyActivity(CouponActivity.class, new Intent().putExtra("totalprice", info.totalprice));

                break;

            case R.id.confirm_order_add_address:

                startVerifyActivity(CreateAddressActivity.class);

                break;

            case R.id.confirm_order_edit_address:

                startVerifyActivity(AddressManageActivity.class);

                break;

            case R.id.confirm_order_coupon_view:

                new WhetherDialog(this, "取消优惠券"){

                    @Override
                    protected void onYes() {

                        couponView.setVisibility(View.GONE);

                        couponPrice.setText("0元");

                        totle.setText(info.totalprice);

                        coupon = new JsonCouponAsyGet.Info.Coupon();

                    }

                }.show();

                break;

            case R.id.confirm_order_confirm:

                if(Double.valueOf(info.yunfei) > Double.valueOf(info.totalprice)){

                    UtilToast.show(this, "未达到起送金额");

                }else if(layout_one.getVisibility() == View.VISIBLE){

                    UtilToast.show(this, "请选择收货地址");

                }else{

                    new JsonOrderresultAsyGet(QSApplication.QSPreferences.readUid(), info.action, info.shopcarid, info.pid, info.gattributeid, info.number, username.getText().toString(), mobile.getText().toString(), location.getText().toString(), coupon.cid, buyremark.getText().toString(), new AsyCallBack<JsonOrderresultAsyGet.Info>(){

                        @Override
                        public void onSuccess(String toast, int type, JsonOrderresultAsyGet.Info info) throws Exception {

                            QSApplication.ShoppingAction.clearData();

                            new PayAction(ConfirmOrderActivity.this){

                                @Override
                                protected void onSuccess() {

                                    startVerifyActivity(CompleteOrderActivity.class);

                                }

                                @Override
                                protected void onEnd() {

                                    if(NavigationActivity.onFragmentChangeListener != null){

                                        NavigationActivity.onFragmentChangeListener.onFragmentToHome();

                                    }

                                    finish();

                                    QSApplication.INSTANCE.finishActivity(ActivitvActivity.class, CommodityDetailActivity.class, ThreeListActivity.class, TwoListActivity.class, CollectionActivity.class, HistoryActivity.class, SearchResultActivity.class, SearchActivity.class);

                                }

                            }.pay(info.ordernumber, QSApplication.QSPreferences.readUid(), info.ordernumber, info.money);

                        }

                    }).execute(this);

                }

                break;

        }

    }

    @Override
    protected void onDestroy() {

        super.onDestroy();

        onOrderChangeListener = null;

    }

    public interface OnOrderChangeListener{

        void onCouponChange(JsonCouponAsyGet.Info.Coupon coupon);

        void onAddressChange(JsonAddressAsyGet.Info.Address address);

    }

}
